"use server";
import { PrismaClient } from "@prisma/client";

// excludeId 是指，除了这个 id 以外的所有数据
export async function queryExist(name, excludeId = null) {
  const prisma = new PrismaClient();

  const driver = await prisma.router.findUnique({
    where: {
      name: name,
    },
  });

  // 排除 excludeId
  if (excludeId) {
    if (driver && driver.id !== excludeId) {
      return {
        status: "error",
        message: "路由名称已存在",
      };
    } else {
      return {
        status: "success",
      };
    }
  }

  if (driver) {
    return {
      status: "error",
      message: "路由名称已存在",
    };
  }

  // return success
  return {
    status: "success",
  };
}

// upsert router
export async function upsertRouter(formData) {
  // 根据 formData 的 name 字段，查询是否存在
  // 如果存在，则更新
  // 如果不存在，则添加
  const name = formData.get("name");

  // query if name exist
  // 查询是否存在
  const exist = await queryExist(name);

  if (exist && exist.status === "error") {
    // 如果存在，则更新
    return await updateRouter(formData);
  } else {
    // 如果不存在，则添加
    return await addRouter(formData);
  }
}

export async function addRouter(formData) {
  const name = formData.get("name");
  const driver = formData.get("driver");
  const api_key = formData.get("api_key");
  const secret_key = formData.get("secret_key");
  const access_token = formData.get("access_token");
  const url = formData.get("url");
  const region = formData.get("region");

  // query if name exist
  const exist = await queryExist(name);

  if (exist && exist.status === "error") {
    return {
      status: "error",
      message: "路由名称已存在",
    };
  }

  const prisma = new PrismaClient();

  // add driver
  const router = await prisma.router.create({
    data: {
      name: name,
      driver: {
        connect: {
          name: driver,
        },
      },
      url: url,
      api_key: api_key,
      secret_key: secret_key,
      access_token: access_token,
      region: region,
    },
  });

  // close prisma
  await prisma.$disconnect();

  // return result
  if (router) {
    return {
      message: "添加成功",
    };
  } else {
    return {
      status: "error",
      message: "添加失败",
    };
  }
}

// get all routers
export async function getRouters() {
  const prisma = new PrismaClient();

  const routers = await prisma.router.findMany({
    include: {
      driver: true,
    },
  });

  await prisma.$disconnect();

  return routers;
}

// delete router
export async function deleteRouter(id) {
  const prisma = new PrismaClient();

  const router = await prisma.router.delete({
    where: {
      id: id,
    },
  });

  await prisma.$disconnect();

  return router;
}

// update router by id and formData
export async function updateRouter(id, formData) {
  const name = formData.get("name");
  const driver = formData.get("driver");
  const api_key = formData.get("api_key");
  const secret_key = formData.get("secret_key");
  const access_token = formData.get("access_token");
  const url = formData.get("url");
  const region = formData.get("region");

  const prisma = new PrismaClient();

  const router = await prisma.router.update({
    where: {
      id: id,
    },
    data: {
      name: name,
      driver: {
        connect: {
          name: driver,
        },
      },
      url: url,
      api_key: api_key,
      secret_key: secret_key,
      access_token: access_token,
      region: region,
    },
  });

  await prisma.$disconnect();

  if (router) {
    return {
      message: "success",
    };
  } else {
    return {
      status: "error",
      message: "更新失败",
    };
  }
}
